package filelog

import (
	"os"
	"path/filepath"

	"sieve_admin_server_userpass/lib/config"
	"sieve_admin_server_userpass/lib/log"
	"sieve_admin_server_userpass/lib/splitfile"
)

var (
	fLogPath    = config.String("log.file.path", filepath.Join(filepath.Dir(os.Args[0]), "../log/"), "log file path")
	fLogName    = config.String("log.file.name", filepath.Base(os.Args[0])+".log", "log file name")
	fLogSplit   = config.String("log.file.split", "none", "log file split: none, daily, monthly")
	fLogReserve = config.Int("log.file.reserve", 0, "log file reserve count")
)

func init() {
	f, err := splitfile.New(&splitfile.Cfg{
		Dir:      *fLogPath,
		FileName: *fLogName,
		Split:    getFileSuffix(),
		Reserve:  *fLogReserve,
	})
	if err != nil {
		log.Fatal(err)
	}
	log.SetOutput(f)
}

func getFileSuffix() string {
	switch *fLogSplit {
	case "none":
		return splitfile.SplitNone
	case "daily":
		return splitfile.SplitDaily
	case "monthly":
		return splitfile.SplitMonthly
	default:
		return *fLogSplit
	}
}
